<?php
class Customer_Model_CustomerBalanceHistory
{
 
	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Customer_Model_DbTable_CustomerBalanceHistory();
	}
	
	/**
	*add One Row  
	*@param array $row 
	*@return boolean
	*/
	public function add($row){
		return $this->_table->insert($row);
	}
	/**
	*Update One Row  
	*@param rowSet $row Customer balance log data 
	*@param int $cbhId 
	*@return boolean
	*/
	public  function update($row,$cbhId){
	
		$where = $this->_table->getAdapter()->quoteInto('cbh_id = ?', $cbhId);
 
		return $this->_table->update($row, $where);
		
	}

	/**
	*get by condation
	*@param int $customer_id 
	*@return boolean
	*/
	public function getByComdation($customerId,$star,$end,$offset,$pageSize,$type,$orderBy=array('cbh_id DESC')){ 
		 
		$select = $this->_table->select();	 
		$select->from($this->_table->info("name"),"$type");
		$select->where('customer_id = ?', $customerId);
		$select->where('cbh_add_time >= ?', $star);
		$select->where('cbh_add_time <= ?', $end);
		$select->limit($pageSize,$offset);
		$select->order($orderBy);
		
		return $this->_table->fetchALL($select);
		 
	}
	/*
	*Get hold balance
	*
	*
	*/
	public function getHold($customerId,$referId,$appType){		
		$select = $this->_table->select();	 
		$select->from($this->_table->info("name"),"*");
		$select->where('customer_id = ?', $customerId);
		$select->where('cbh_refer_id = ?', $referId);
		$select->where('cbh_op_type = ?', $appType);
		$select->where('cbh_class = 1');
		return $this->_table->getAdapter()->fetchAll($select);
	}
	
	/*
	*lock balance history row
	*
	*
	*/
	public function lockHistory($cbhId){		
	 
		$sql = "select * from customer_balance_history where cbh_id='$cbhId' for update";
		return $this->_table->getAdapter()->fetchRow($sql);
	
	}
 
}